#include "handles.h"
#include "util.h"
#include "protocol.h"

// BAKERY:
#include "uart.h"

void default_handle(unsigned int *data, unsigned char size, unsigned type, unsigned char seq_num);

void default_handle_node_alive(unsigned int *data, unsigned char size, unsigned char seq_num) {
    default_handle(data, size, MESG_TYPE_NODE_ALIVE, seq_num);
}

void default_handle_pir_detection(unsigned int *data, unsigned char size, unsigned char seq_num) {
    default_handle(data, size, MESG_TYPE_PIR_DETECTION, seq_num);
}

handle_function HANDLE_FUNCTIONS[] = {
    &default_handle_node_alive,
    &default_handle_pir_detection,
};

void default_handle(unsigned int *data, unsigned char size, unsigned type, unsigned char seq_num) {
    unsigned char checksum = 0;
    writeStr("#");
    writeStr(itoa(type, 10));
    checksum += type;
    writeStr("|");
    writeStr(itoa(seq_num, 10));
    checksum += seq_num;
    writeStr("|");
    writeStr(itoa(size, 10));
    checksum += size;
    writeStr("|");
    while (size) {
        writeStr(itoa((*data), 10));
        checksum += (*data);
        data++;
        size--;
        writeStr("|");
    }
    writeStr(itoa(checksum, 10));
    writeStr("|");
    writeStr("\r\n");
}
